<?php echo $this->headLink()->appendStylesheet($this->Css('jtip.css'))?>
<?php echo $this->headScript()->appendFile($this->js('jquery.js'))
	->appendFile($this->js('plugins/jquery.template.js'))
	->appendFile($this->js('plugins/jquery.form.js'))
	->appendFile($this->js('plugins/jquery.blockUI.js'))
	->appendFile($this->js('plugins/jtip.js'))
	->captureStart();
?>
var all_class = 1; // 各班設定
var grade_data = null; // 各年級
var class_data = null; // 班名
var subject_data = null; // 科目名
var yetset_subject = null;
$(document).ready(function(){
	initGrade();
	initData();
	initSubjectData();

	$("#enbtn").click(function(){
		if (all_class){
			$(this).css('background-color','#fe8').val('<?php echo $this->translate('return whole class set')?>');
			all_class = 0;
			alert('<?php echo $this->translate('If the same course with the grade, do not set this item')?>');
		}
		else {
			$(this).css('background-color','').val('<?php echo $this->translate('Special classes Set')?>');
			all_class = 1;
		}
		initClass();
		initSubjectData();
		initSubject();

		$("#sel_class").change(function(){
			initSubjectData();
			initSubject();
		});
	});


	$("#sel_class").change(function(){
		initSubject();
	});

	$("#year_semester").change(function(){

		initClass();
		initSubjectData();
		initSubject();
	});

	var options = {
	    beforeSubmit: function(){
	    $('div.main_body').block({message:'<h1><?php echo $this->translate('data setting..')?></h1>',  border: '3px solid #f00' });
	    },
        success: function(data){
        $('div.main_body').unblock();
        initSubjectData();
        }
	}

	$('#myform').ajaxForm(options);

	// 出所有科目
	$("#list_all_subject").click(list_all_subject);
	// 	加權同節數
	$("#rate_same_sections").click(rate_same_sections);
});

	// 	加權同節數
	var rate_same_sections =  function(){
		var year_semester = $("#year_semester").val();
		var sel_grade = $("#sel_grade").val();
		var class_sn = $("#sel_class").val();
		if (class_sn)
			var url = '<?php echo $this->baseScript ?>/rate2sections/year_semester/'+year_semester+'/grade/'+sel_grade+'/class_sn/'+class_sn;
			else
			var url = '<?php echo $this->baseScript ?>/rate2sections/year_semester/'+year_semester+'/grade/'+sel_grade;
			$('div.main_body').block({message:'<h1><?php echo $this->translate('data setting..')?></h1>',  border: '3px solid #f00' });
			$.getJSON(url,function(data){
				initSubjectData();
				$('div.main_body').unblock();
			});

	};

// 列出所有科=============
	function list_all_subject (){
		$('div.main_body').block({message:'<h1><?php echo $this->translate('data setting..')?></h1>',  border: '3px solid #f00' });
		var year_semester = $("#year_semester").val();

		$("#addlist").show();
		$("#settable").hide();
		$("#settable tbody").empty();
		var nor =  '<td >${name}</td><td id="s1_${id}">${scored}</td><td id="s2_${id}">${scored_mode}</td><td id="s3_${id}">${sections}</td><td id="s4_${id}">${rate}</td>';
		var t1 = $.template('<tr id="${id}"><td rowspan="${i}" style="text-align:right">${scope}</td>'+nor+'</tr>${appendRow}');
		var t2 = $.template('<tr id="${id}">'+nor+'</tr>${appendRow}');

		$.each(class_data,function(ii,vv){
			var r =0;
			var total_sections = 0;
			// 取得學科資料
			if (all_class == 0)
			var url = '<?php echo $this->baseScript ?>/getsubject/year_semester/'+year_semester+'/grade/'+ii+'/class_sn/'+$("#sel_class").val();
			else
			var url = '<?php echo $this->baseScript ?>/getsubject/year_semester/'+year_semester+'/grade/'+ii;

			$("#addlist").append("<table  id='t-"+ii+"'><thead><tr><th colspan='6'>"+grade_data[ii]+
			"<?php echo $this->translate('subject')?>  <span class='tol_sec' val2='"+ii+"'></span>"+
			" <span class='setbtn' val='"+ii+"'><?php echo $this->translate('edit subject')?></span></th>"+
			"</tr><tr><th><?php echo $this->translate('scope')?></th>"+
			"<th><?php echo $this->translate('subject name')?></th>"+
			"<th><?php echo $this->translate('scored')?></th>"+
			"<th><?php echo $this->translate('full')?></th>"+
			"<th><?php echo $this->translate('sections')?></th>"+
			"<th><?php echo $this->translate('rate')?></th>"+
			"</tr></thead><tbody></tbody></table>");
			var stbody = $("#t-"+ii+" tbody");

			$.getJSON(url,function(data){

				var s_data = data.data;
				var y_subject = data.yetset;

				$.each(s_data,function(s,v){
					var vlen = v.length;

					for(i=0; i < v.length ;i++){
						if (i == 0) var st = t1; else st = t2;
						var scope_rank = v[i].scope_rank;
						var rank = v[i].rank;

					if (vlen>1)
					var arank = scope_rank+'-'+rank;
					else
				 	var arank = scope_rank;


					if (v[i].scored == 'Y')
						var scored = "<img  src='<?php echo $this->baseUrl ?>/images/ok.png' />"
					else
						scored = '';
					if (v[i].scored_mode == '1')
						var scored_mode = "<img src='<?php echo $this->baseUrl ?>/images/ok.png' />"
					else
						var scored_mode = '';

						stbody.append(st, {
						id   : v[i].sn,
						i	 : vlen,
						sn   : v[i].sn,
						scope: s ,
						scored : scored,
						scored_mode : scored_mode,
						name : v[i].name,
						sections: v[i].sections,
						rate : v[i].rate,
						rank : arank
					});
					total_sections = total_sections+parseInt(v[i].sections);
				}

				});
				$("span[val2="+ii+"]").html('<?php echo $this->translate('section total')?>: '+total_sections+'<?php echo $this->translate('section')?>');
				$("tbody tr:even").css("background", "#fee");
			});

		});
		// 編修
		$("#addlist span").click(function(){
			var grade =$(this).attr('val');

			$("#addlist").hide();
			$("#settable").show();
			$("#list_all_subject").show();
			$("#sel_grade").val(grade);
			$("#sel_grade").trigger('change');
			$("#addlist").empty();
		});


		$(this).hide();

		$('div.main_body').unblock();
	}



// 初始化年級 ============
function initGrade(){
	var url = '<?php echo $this->baseScript ?>/getgrade/';
	$.getJSON(url,function(data){
		grade_data = data;
		$.each(data,function(i,v){
			$("#sel_grade").append('<option value="'+i+'">'+v+'</option>');
		});
	});
	$("#sel_grade").change(function(){
		initClass();
		initSubjectData();
	});

}

// 初始化資料 ===========
function initData(){
	var year_semester = $("#year_semester").val();
	var url = '<?php echo $this->baseScript ?>/getyear/year_semester/'+year_semester;
	$.getJSON(url,function(data){
		class_data = data;
		initClass();
	});
}

// 初始化學科資料  ==========
function initSubjectData(){
	var year_semester = $("#year_semester").val();
	if ($("#sel_grade").val())
		var sel_grade = $("#sel_grade").val();
	else
		var sel_grade = 1;

	// 取得學科資料
	if (all_class == 0)
	var url = '<?php echo $this->route('getsubject') ?>/year_semester/'+year_semester+'/grade/'+sel_grade+'/class_sn/'+$("#sel_class").val();
	else
	var url = '<?php echo $this->route('getsubject')?>/year_semester/'+year_semester+'/grade/'+sel_grade;
	$.getJSON(url,function(data){
		subject_data = data.data;
		yetset_subject = data.yetset;
		initSubject();
	});

}

// 初始化班級 ============
function initClass() {
	$("#sel_class").empty();
	var year_semester = $("#year_semester").val();
	if ($("#sel_grade").val())
		var sel_grade = $("#sel_grade").val();
	else
		var sel_grade = 1;
	if (all_class){  //全年級設定
		$("#sel_class").hide();
	} else {
		$("#sel_class").show();
		var data = class_data[sel_grade];
		var t1 = $.template("<option value='${id}'>${val}</option>");
		$.each(data ,function(){
			$("#sel_class").append(t1,{
				id  : this.class_sn,
				val : this.chinese_name+'<?php echo $this->translate('class')?>'
				});
			});
	}
}


// 初始化科目 =========
function initSubject(){

	$("#settable  tbody").empty();
	if ($("#sel_class").val())
		var title = $("#sel_grade :selected").text()+$("#sel_class :selected").text();
	else
		var title = $("#sel_grade :selected").text()+'<?php echo $this->translate('whole grade')?>';

	$("#subtitle").html(title+' <?php echo $this->translate('subject setup')?>');

	var nor =  '<td >${name}</td><td id="s1_${id}">${scored}</td><td id="s2_${id}">${scored_mode}</td><td id="s3_${id}">${sections}</td><td id="s4_${id}">${rate}</td><td sn="${sn}">${editbtn} ${delbtn} </td>';
	var t1 = $.template('<tr id="${id}"><td rowspan="${i}" style="text-align:right">${scope}</td>'+nor+'</tr>${appendRow}');
	var t2 = $.template('<tr id="${id}">'+nor+'</tr>${appendRow}');

	var r =0;
	var total_sections = 0;
	$("#total_sections").hide();
	$("#rate_same_sections").hide();
	$.each(subject_data,function(s,v){
		vlen = v.length;

		for(i=0; i < v.length ;i++){
			if (i == 0) var st = t1; else st = t2;

			var scope_rank = v[i].scope_rank;
			var rank = v[i].rank;

			if (vlen>1){
				var arank = scope_rank+'-'+rank;
			} else {
			 	var arank = scope_rank;
			}
			var delbtn = '<a href="#" type="delete" class="delete"><?php echo $this->translate('delete')?></a>';
			if (v[i].scored == 'Y')
				var scored = "<img  src='<?php echo $this->baseUrl ?>/images/ok.png' />"
			else
				scored = '';
			if (v[i].scored_mode == '1')
				var scored_mode = "<img src='<?php echo $this->baseUrl ?>/images/ok.png' />"
			else
				var scored_mode = '';

			$("#settable  tbody").append(st, {
				id   : v[i].sn,
				i	 : vlen,
				sn   : v[i].sn,
				scope: s ,
				editbtn  : '<a href="#" type="edit"><img src="<?php echo $this->baseUrl ?>/images/edit.png"> <?php echo $this->translate('edit')?></a>',
				scored : scored,
				scored_mode : scored_mode,
				name : v[i].name,
				sections: v[i].sections,
				rate : v[i].rate,
				rank : arank,
				delbtn: delbtn
				});
			total_sections = total_sections+parseInt(v[i].sections);
		}

	});
	if (total_sections > 0)
		$("#rate_same_sections").show();
	// 總節數
	$("#total_sections").html('<?php echo $this->translate('section total')?> : '+ total_sections+'<?php echo $this->translate('section')?>').show();

	// 刪除處理
	$("#settable a[type='delete']").click(function(){
		if (confirm('<?php echo $this->translate('confirm delete?')?>')){
			var id = $(this).parents("tr").attr('id');
			var url = '<?php echo $this->baseScript ?>/deletesubject/sn/'+id;
			$.getJSON(url,function(data){
			    if (data == false)
			    alert('<?php echo $this->translate('There Course Scheduling information can not be deleted')?>');
			    else
				initSubjectData();
			});
		}
	});

	// 修改處理
	$("#settable a[type='edit']").click(function(){
		//$(this).parents("tr").css('background','yellow');
		//$(".like_button").parent().empty();
		var id = $(this).parents("tr").attr('id');

		// 計分
		if ($("#s1_"+id).html()!='') {
			$("#s1_"+id).html('<input type="checkbox" name="scored" checked value="1" />');
		}
		else {
			$("#s1_"+id).html('<input type="checkbox" name="scored"  value="1" />');
		}

		// 完整
		if ($("#s2_"+id).html()!='') {
			$("#s2_"+id).html('<input type="checkbox" name="scored_mode" checked value="1" />');
		}
		else {
			$("#s2_"+id).html('<input type="checkbox" name="scored_mode"  value="1" />');
		}
		// 節數
		$("#s3_"+id).html('<input type="text" size="2" name="sections" value="'+$("#s3_"+id).text()+'">');

		// 節數
		$("#s4_"+id).html('<input type="text" size="2" name="rate" value="'+$("#s4_"+id).text()+'">');

		$("#settable a ").unbind('click');

		$(this).parent().html('<input type="hidden" name="sn" value="'+id+'" /><input type="submit" id="do_edit" value="<?php echo $this->translate('setup')?>" /> <input type="button" id="cancel_edit" value="<?php echo $this->translate('cancel')?>" />');



		$("#cancel_edit").click(function(){
			initSubject();
		});
	});

	$("#settable  tr:even").css("background", "#fee");

	// 未設定科目
	if(yetset_subject ){
		$("#addtable").show();
		$("#addtable ul").empty();
		var t2 = $.template('<li id="y-${id}"><a href="#"><?php echo $this->translate('join')?></a> ${name} ${memo}</li>');
		$.each(yetset_subject,function(i,v){
			var memo='';
			if (v.special == 'Y')
				var memo = '(<?php echo $this->translate('special class')?>)';
			$("#addtable ul").append(t2,{
				id: i,
				name : v.name,
				memo : memo
			});
		});
		if ($("#addtable ul").text())
		$("#addtable").show();
		else
		$("#addtable").hide();


		//  加入一科
		var  addOneSubject = function(){
			var id = $(this).parent().attr("id");
			var year_semester = $("#year_semester").val();
			var sel_grade = $("#sel_grade").val();
			var class_sn = $("#sel_class").val();
			if (class_sn)
			var url = '<?php echo $this->baseScript ?>/setOneSubject/year_semester/'+year_semester+'/grade/'+sel_grade+'/class_sn/'+class_sn+'/sn/'+id;
			else
			var url = '<?php echo $this->baseScript ?>/setOneSubject/year_semester/'+year_semester+'/grade/'+sel_grade+'/sn/'+id;
			$.getJSON(url,function(data){
			     if (data.error)
			     alert('<?php echo $this->translate('There Course Scheduling information , can not be added')?>');
			     else
				    initSubjectData();
			});
		}

		// 加入一科 click 事件
		$("#addtable li a").click(addOneSubject);
	}




}

<?php echo $this->headScript()->captureEnd() ?>

<?php echo $this->headStyle()->captureStart() ?>

	#settable {float:left;width:80%;margin:5px}
	#settable table {width:100%}
	#addlist table {width:85%;background:#ccc;margin:5px auto }
	#addlist tr {background:#fff;padding:2px}
	#addlist td,#settable td {padding:3px;text-align:center}
	#right_message{padding:1px;float:left;width:150px;margin:3px}
	#settable {background:#ccc}
	#settable tr{background:#fff}
	#settable img {border:0px}
	#addtable {background:#efe;display:none}
	#addtable h1 {font-size:120%;text-align:center; border:3px solid #0de; background:#efd}
	#addtable li {list-style-type:decimal}
	#addtable a {background:#ff0;color:#00f}
	#addtable hover {color:#fe0;text-decoration:underline;}
	.tol_sec,#total_sections{margin:5px 3px;padding:2px; border:1px solid #dde; background:#ffb}
	#list_all_subject{margin:5px auto;padding:2px;width:100px}
	.setbtn, #rate_same_sections{border:1px double #dde;background:#ffb; float:right;cursor: pointer}
<?php echo $this->headStyle()->captureEnd() ?>


<div class="main_body">
<form action="<?php echo $this->baseScript?>/edit" method="post" id="myform">
<div id="settable">
<table width="85%" >
<thead>
<tr>
<th colspan="8" style="text-align:center" id="sethead">
<div>
<div style="float:left;text-align:center;">
<select name="year_semester" class="refresh" id="year_semester">
<?php foreach($this->YearSemester() as $val): ?>
	<?$id = $val['year'].'-'.$val['semester'] ?>
	<option value="<?php echo $id?>" <?php if ($id == $this->year_semester): ?>selected<?php endif ?> >
	<?php echo sprintf($this->translate('%dyear%dsemester'),$val['year'],$val['semester'])?></option>
<?php endforeach; ?>
</select>
<select name='sel_grade' id="sel_grade" class="refresh" >

</select> <select name="class_sn" id="sel_class" style="display:none"></select>
 <?php echo $this->translate('subject setup list')?>
</div>
<div style="float:right"><input id="enbtn" type="button"  value="<?php echo $this->translate('special class subject')?>" /></div>
</div>
</th>
</tr>

<tr>
<th colspan="8">
<div style="float:left;text-align:center;width:80%"><span id="subtitle"></span> <span id="total_sections"></span></div> <span id="rate_same_sections" title="本學期所有加權數與節數相同">加權同節數</span>
</th>
</tr>
<tr>
<th><?php echo $this->translate('scope')?></th>
<th><?php echo $this->translate('subject name')?></th>
<th><?php echo $this->translate('scored')?></th>
<th><?php echo $this->translate('full')?></th>
<th><?php echo $this->translate('sections')?></th>
<th><?php echo $this->translate('rate')?></th>
<th><?php echo $this->translate('edit')?></th></tr>
</thead>
<tbody>

</tbody>
</table>
</div>
<div id="addlist" style="display:none"></div>

</form>
<div id="right_message">
<div id="list_all_subject" class="like_button">
<a href="#"><?php echo $this->translate('all subject list')?></a>
</div>


<div id="addtable"  >
<h1><?php echo $this->translate('yet join subject')?></h1>
<ul>
</ul>
</div>
</div>
</div>
<div style="clear:both"></div>